package com.example.yimai.controller;


import com.example.yimai.dto.PageResponseDTO;
import com.example.yimai.entity.News;
import com.example.yimai.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/news")
public class NewsController {

    @Autowired
    private NewsService newsService;

    @GetMapping("/query")
    public PageResponseDTO<News> queryNews(@RequestParam(required = false) String keyword,
                                           @RequestParam(defaultValue = "1") int pageNum,
                                           @RequestParam(defaultValue = "10") int pageSize){
        return newsService.queryNews(keyword, pageNum-1, pageSize);
    }

    @RequestMapping("/getLatest")
    public List<News> getLatestNews(){
        return newsService.getLatestNews();
    }

    /**
     * 添加资讯
     * @param title 资讯标题
     * @param content 资讯内容
     * @return
     */
    @PostMapping("/add")
    public String addNews(@RequestParam String title,
                                          @RequestParam String content){
        return newsService.addNews(title, content);
    }

    @DeleteMapping("/delete/{id}")
    public String deleteNews(@PathVariable Integer id){
        return newsService.deleteNews(id);
    }

    @PutMapping("/update")
    public News updateNews(@RequestParam String id,
                           @RequestParam String title,
                           @RequestParam String content){
        News news = new News();
        news.setId(id);
        news.setTitle(title);
        news.setContent(content);
        return newsService.updateNews(news);
    }

}
